package zhtt.mongo01;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by mengxiaxia on 2016/11/13.
 */
@SpringBootApplication
@RestController
public class ControllerTest {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/save/{id}/{username}/{age}")
    private String save(@PathVariable("id")long id,@PathVariable("username")String username,@PathVariable("age")int age){
        try{
            userRepository.save(new User(id, username, age));
            return "success";
        }catch (Exception e){
            return e.getMessage();
        }
    }

    @RequestMapping("/get/{id}")
    private String get(@PathVariable("id")long id){
        try{
            return userRepository.findOne(id).toString();
        }catch (Exception e){
            return e.getMessage();
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(ControllerTest.class, args);
    }
}
